<template>
  <!-- 竞猜计算器 -->
  <div class="mainwrap">
    <!-- tab切换 -->
    <div class='tabar-con'>
      <van-icon name="arrow-left" class="back" @click="b.back" />
      <div class='tab-box'>
        <div :class='type === "z" ? "active" : ""' @click='checkTab("z")'>足球</div>
        <div :class='type === "l" ? "active" : ""' @click='checkTab("l")'>篮球</div>
      </div>
    </div>
    <!-- 足球比赛列表 -->
    <div class='tabBar' v-if='type === "z"'>
      <component v-if="footBalls.length" :is="FootballCom"></component>
      <van-empty description="暂无比赛数据" v-if="!footBalls.length"></van-empty>
    </div>
    <!-- 篮球比赛列表 -->
    <div class='tabBar' v-if='type === "l"'>
      <keep-alive v-if="basketBalls.length">
        <component :is="BasketballCom"></component>
      </keep-alive>
      <van-empty description="暂无比赛数据" v-if="!basketBalls.length"></van-empty>
    </div>
    <!-- 奖金优化底部功能按钮 -->
    <Counter v-if="Object.keys(orderData).length" :type="type" @changeType="changeType" />
  </div>
</template>

<script setup>
import { onBeforeRouteLeave } from 'vue-router'
import { storeToRefs } from 'pinia'
import { useCalcStore, useUserStore, useGlobeStore } from "@/stores";
import { referenceBonusMatches } from '@/api/calc'
import useBack from '@/hooks/back';
const FootballCom = defineAsyncComponent(() => import('@/components/calc/football-index.vue'))// 足球组件
const BasketballCom = defineAsyncComponent(() => import('@/components/calc/basketball-index.vue'))// 篮球组件
const b = useBack();
const user = useUserStore()
const globe = useGlobeStore();
const calc = useCalcStore() // 竞猜store
const { orderData, footBalls, basketBalls } = storeToRefs(calc)
const type = ref('z')

// 重置页面数据
function resetData() {
  calc.saveData({})
  calc.changeOdds(1)
}

const checkTab = (type1) => {
  if (type1 === type.value) return
  type.value = type1
  resetData()
}

function changeType(val) {
  type.value = val
}

onBeforeMount(async()=>{
  const res = await referenceBonusMatches({ i: user.i, cid: globe.cid})
  if (res) {
    calc.setMatchDataCalcBonus(res.matches)
  }
})
onBeforeRouteLeave((to, from, next) => {
  resetData()
  next()
})
</script>

<style lang="less" scoped>
// 所有的rem单位都得 x*100/2
.mainwrap {
  position: relative;
  border-radius: 12px 12px 0 0;
  background-color: #fff;
  --van-collapse-item-content-background: #eee;

  .tabBar{
    height: calc(100vh - 50px);
  }
  .tabar-con {
    width: 100%;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    .back{
      position: absolute;
      left: 10px;
      top: 50%;
      transform: translateY(-50%);
      font-size: 20px;
    }
    .tab-box {
      display: flex;
      width: 136px;
      height: 27px;
      border: 1px solid #FF5B35;
      border-radius: 5px;
      overflow: hidden;

      div {
        flex: 1;
        color: #FF5B35;
        text-align: center;
        line-height: 26px;
        font-size: 13px;
        font-weight: bold;
      }

      .active {
        background: #FF5B35;
        color: #fff;
      }
    }
  }

  .tabBar {
    width: 100%;
    border-top: 1px solid #eee;
    --van-collapse-item-content-background-color: #eee;
    --van-collapse-item-content-padding: 10px;
    --van-collapse-item-content-font-size: 12px;
  }

  &.animate {
    animation-name: AniFadeInUp;
    animation-duration: 1s;
    animation-fill-mode: both;
  }
}
</style>
